using System;
using System.Collections.Generic;
using System.Text;

namespace HexterConsulting
{
    class PerformanceLog
    {
        private string _filename;
        private System.Collections.ArrayList _fieldNames = new System.Collections.ArrayList();
        private System.Collections.ArrayList _categoryNames = new System.Collections.ArrayList();
        private System.Collections.ArrayList _counterNames = new System.Collections.ArrayList();
        private System.Collections.ArrayList _serverNames = new System.Collections.ArrayList();
        private System.Collections.ArrayList _instanceNames = new System.Collections.ArrayList();
        private System.Collections.Generic.Dictionary<string, Category> _categories = new Dictionary<string, Category>();
        private string timestamp;


        public PerformanceLog(string Filename)
        {
            _filename = Filename;
            Load();
        }

        public bool FieldExists(string FieldName)
        {
            return this._fieldNames.Contains("\""+FieldName+"\"");
        }

        public string Filename
        {
            get
            {
                return _filename;
            }
        }
        public string TimeFieldName
        {
            get
            {
                return timestamp;
            }
        }

        public string[] Servers
        {
            get
            {
                return (string[])_serverNames.ToArray(typeof(string));
            }
        }
        public System.Collections.Generic.Dictionary<string,Category> Categories
        {
            get
            {
                return _categories;
                //new System.Collections.ObjectModel.Collection<Category>(_categories.Values);
                //System.Collections.ObjectModel.Collection<Category>  
                    //new System.Collections.ObjectModel.Collection<Category>( _categories.Values);
            }
        }

        internal static string FieldName(string MachineName,string CategoryName, string InstanceName,string CounterName)
        {
            string  fieldNameFormat = @"\\{0}\{1}{2}\{3}";   
            string instance;
            
            if (InstanceName != null && InstanceName.Length > 0)
                instance = "(" + InstanceName + ")";
            else
                instance = string.Empty;

            return string.Format(fieldNameFormat,MachineName,CategoryName,instance,CounterName);
        }

        private void Load()
        {
            string[] files = System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(_filename),System.IO.Path.GetFileName(_filename));
            using (System.IO.StreamReader fileReader = System.IO.File.OpenText(files[0]))
            {
                if (fileReader != null)
                {
                    string header = fileReader.ReadLine();
                    int fieldcount = 0;
                    string[] fields = header.Split(',');
                    Array.Sort(fields);
                    foreach (string field in fields)
                    {
                        fieldcount++;
                        if (fieldcount == 1)
                        {
                            timestamp = field.Remove(0, 1);
                            timestamp = timestamp.Remove(timestamp.Length - 1);
                        }
                        else
                        {
                            string tmp = field.Remove(0, 2);
                            tmp = tmp.Remove(tmp.Length - 1);
                            _fieldNames.Add(field);
                            string[] counter = tmp.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
                            string server = counter[0]; 
                             string category ;
                             string instance = null;
                             if (counter[1].Contains("("))
                             {
                                 category = counter[1].Substring(0, counter[1].IndexOf('('));
                                 instance = counter[1].Substring(category.Length + 1, counter[1].Length - category.Length - 2);
                             }
                             else
                             {
                                 category = counter[1];
                             }
                            
                            string countername = counter[2];
                            
                            if (!_serverNames.Contains(server))
                            {
                                _serverNames.Add(server);
                            }
                             
                            if(instance!=null && !_instanceNames.Contains(instance))
                            {
                                _instanceNames.Add(instance);
                            }

                            if (!_categoryNames.Contains(category))
                            {
                                _categoryNames.Add(category);
                                _categories.Add(category, new Category(category));
                            }
                             
                            ((Category)_categories[category]).AddCounter(countername);
                            ((Category)_categories[category]).AddInstance(instance);
                            ((Category)_categories[category]).AddMachine(server);

                            if (!_counterNames.Contains(countername))
                            {
                                _counterNames.Add(countername);
                            }
                        }
                    }
                }
                
            }          
        }

    }
}
